package floappcontest.core.backbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import floappcontest.core.beans.TeachingInstitution;
import floappcontest.core.beans.Tutor;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;
import floappcontest.util.converters.TIConverter;


/**
 * The TeamBean is a BackBean (JavaBean) related with the tutor events (insert,
 * delete, edit, etc) in the pages.
 */
public class TutorBean {

	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Control control;

	private Tutor tutor;

	private List<Tutor> list;

	private List<SelectItem> tiList;

	private TIConverter converter;


	/**
	 * 
	 */
	public TutorBean() {
		setControl( new Control( new HibernateDAOFactory() ) );
		setTutor( new Tutor() );
		getTutor().setIes( new TeachingInstitution() );
		refreshList();
		listTI();
	}


	private void listTI() {
		List<TeachingInstitution> tis = getControl().listTeachingInstitutions();
		setTiList( new ArrayList<SelectItem>() );
		for ( TeachingInstitution ti : tis ) {
			getTiList().add( new SelectItem( ti, ti.getName()) );
		}

	}


	/**
	 * @return
	 */
	public Tutor getTutor() {
		return this.tutor;
	}


	/**
	 * @param tutor
	 */
	public void setTutor( Tutor tutor ) {
		this.tutor = tutor;
	}


	/**
	 * Navigation handling method (Action methods) that insert a tutor.
	 * 
	 * @return "OK" if the insertion occur without problems, otherwise "FAILED".
	 */
	public String insertTutor() {
		try {
			getControl().insertTutor( getTutor() );
			setTutor( new Tutor() );
			return OK;
		} catch ( FloAppException e ) {
			setTutor( new Tutor() );
			return FAILED;
		} finally {
			refreshList();
		}
	}


	/**
	 * @return
	 */
	public List<Tutor> list() {
		return getControl().listTutors();
	}


	/**
	 * @return
	 */
	public Control getControl() {
		return this.control;
	}


	/**
	 * @param control
	 */
	public void setControl( Control control ) {
		this.control = control;
	}


	/**
	 * @return
	 */
	public void refreshList() {
		setList( getControl().listTutors() );
	}


	public List<Tutor> getList() {
		return this.list;
	}


	public void setList( List<Tutor> list ) {
		this.list = list;
	}


	public String removeTutor() {
		try {
			getControl().removeTutor( getTutor().getLogin() );
			return new MenuBarRedirection().listTutorPage();
		} catch ( FloAppException e ) {
			e.printStackTrace();
			return FAILED;
		}
	}


	public TIConverter getConverter() {
		listTI();
		this.converter = new TIConverter( getTiList() );
		return this.converter;
	}


	public void setConverter( TIConverter converter ) {
		this.converter = converter;
	}


	public List<SelectItem> getTiList() {
		return this.tiList;
	}


	public void setTiList( List<SelectItem> tiList ) {
		this.tiList = tiList;
	}

}